package com.lc.zpyh.ui.activity.order;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.zpyh.R;
import com.lc.zpyh.aop.SingleClick;
import com.lc.zpyh.app.AppActivity;
import com.lc.zpyh.http.glide.GlideApp;
import com.lc.zpyh.http.request.ConfirmReceiptApi;
import com.lc.zpyh.http.request.PublicMsgBean;
import com.lc.zpyh.http.request.SelectReceiveOrderByCabinetryIdApi;
import com.lc.zpyh.http.response.SelectReceiveOrderByCabinetryIdBean;
import com.lc.zpyh.other.IntentKey;
import com.lc.zpyh.util.BigDecimalUtils;
import com.lc.zpyh.util.SPUtil;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
/**
 * 首页扫码跳转
 */

public class OrderTakeMealActivity extends AppActivity {
    @BindView(R.id.rv_shitang)
    RecyclerView rvShitang;
     private String cabinetryId;
    private String qrCodeType;
     private List<SelectReceiveOrderByCabinetryIdBean.ListBean> listdata=new ArrayList<>();
    private BaseQuickAdapter<SelectReceiveOrderByCabinetryIdBean.ListBean,BaseViewHolder> adapter;
    private  BaseQuickAdapter<SelectReceiveOrderByCabinetryIdBean.ListBean.DetailsBean, BaseViewHolder>    goodsAdp;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_ordertakemeal;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        cabinetryId=   getIntent().getStringExtra("cabinetryId");
        qrCodeType=   getIntent().getStringExtra("qrCodeType");


        adapter = new BaseQuickAdapter<SelectReceiveOrderByCabinetryIdBean.ListBean, BaseViewHolder>(R.layout.item_ordertakemeal,listdata) {
            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, SelectReceiveOrderByCabinetryIdBean.ListBean listBean) {
                baseViewHolder.setText(R.id.tv_order_time,listBean.getPlaceOrderTime());
                baseViewHolder.setText(R.id.tv_order_tag,"订单编号："+listBean.getOrderId()+"");
                baseViewHolder.setText(R.id.tv_shangjiamingcheng,listBean.getMerchantName());
                   baseViewHolder.getView(R.id.tv_sure).setOnClickListener(new View.OnClickListener() {
                       @SingleClick
                       @Override
                       public void onClick(View view) {
                           confirmReceipt(listBean.getOrderId()+"");
                       }
                   });
                RecyclerView rv_order_goods=baseViewHolder.getView(R.id.rv_shangjia);
              goodsAdp =new BaseQuickAdapter<SelectReceiveOrderByCabinetryIdBean.ListBean.DetailsBean, BaseViewHolder>(R.layout.item_shop_confirm_shangjia_order,listBean.getDetails()) {
                    @Override
                    protected void convert(@NotNull BaseViewHolder baseViewHolder, SelectReceiveOrderByCabinetryIdBean.ListBean.DetailsBean bean) {
                        baseViewHolder.setText(R.id.goods,bean.getCommodityName());
                        baseViewHolder.setText(R.id.tv_num,"x"+bean.getCount());
                        baseViewHolder.setText(R.id.tv_yueshou,bean.getSpecificationName());

                        baseViewHolder.setText(R.id.tv_xianjia,"￥"+ BigDecimalUtils.div(bean.getCommodityPrice()+"","100",2));
                        GlideApp.with(getActivity())
                                .load(IntentKey.ORDER_IMAGE+bean.getMainImg())
                                .transform(new MultiTransformation<>(new CenterCrop(), new CircleCrop()))
                                .into((ImageView) baseViewHolder.getView(R.id.img_dianpu));

                    }
                };

                rv_order_goods.setAdapter(goodsAdp);
            }

        };
        View emptyview = View.inflate(OrderTakeMealActivity.this, R.layout.empty_emptylayout, null);

        adapter.setEmptyView(emptyview);


        rvShitang.setAdapter(adapter);
        selectReceiveOrderByCabinetryId(cabinetryId);


    }

    private void confirmReceipt(String orderID) {
        EasyHttp.post(this)
                .api(new ConfirmReceiptApi().setOrderId(orderID).setCabinetryId(cabinetryId).setUserId(SPUtil.get(this, IntentKey.USERID,""))).request(new HttpCallback<PublicMsgBean>(this) {

            @Override
            public void onSucceed(PublicMsgBean data) {
//
                if (data.getCode() == 0) {
                    selectReceiveOrderByCabinetryId(cabinetryId);
                }
                 toast(data.getList());

            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }

    private void selectReceiveOrderByCabinetryId(String cabinetryId) {
        EasyHttp.post(this)
                .api(new SelectReceiveOrderByCabinetryIdApi().setCabinetryId(cabinetryId).setUserid(SPUtil.get(this, IntentKey.USERID,""))).request(new HttpCallback<SelectReceiveOrderByCabinetryIdBean>(this) {

            @Override
            public void onSucceed(SelectReceiveOrderByCabinetryIdBean data) {
//
                if (data.getCode() == 0) {
                    listdata=data.getList();
                    adapter.setNewData(listdata);
                    adapter.notifyDataSetChanged();
                }


            }

            @Override
            public void onFail(Exception e) {
                super.onFail(e);

            }
        });
    }
}
